package com.example.ymm.modules1.service.impl.sys;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.sys.SysApprovalSettingLine;
import com.example.ymm.modules1.entity.my.sys.SysApprovalSettingNode;
import com.example.ymm.modules1.entity.my.user.KqDept;
import com.example.ymm.modules1.mapper.my.sys.SysApprovalSettingLineMapper;
import com.example.ymm.modules1.query.sys.SysApprovalSettingLineQueryCriteria;
import com.example.ymm.modules1.query.sys.SysApprovalSettingNodeQueryCriteria;
import com.example.ymm.modules1.service.hl.kq.KqDeptService;
import com.example.ymm.modules1.service.sys.SysApprovalSettingLineService;
import com.example.ymm.modules1.service.sys.SysApprovalSettingNodeService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@DS("slave2")
@Slf4j
public class SysApprovalSettingLineServiceImpl extends BaseServiceImpl<SysApprovalSettingLineMapper, SysApprovalSettingLine> implements SysApprovalSettingLineService {

    @Autowired
    SysApprovalSettingNodeService sysApprovalSettingNodeService;

    @Autowired
    KqDeptService kqDeptService;

    @Override
    public TableList<SysApprovalSettingLine> list(SysApprovalSettingLineQueryCriteria criteria, YmPageDto pageDto) {
        IPage<SysApprovalSettingLine> page = new Page<SysApprovalSettingLine>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysApprovalSettingLine.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<SysApprovalSettingLine> tableList=new TableList(page);
        return tableList;
    }

    public List<SysApprovalSettingLine> queryAll(SysApprovalSettingLineQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysApprovalSettingLine.class, criteria);
        predicate.orderByAsc("sort");
        List<SysApprovalSettingLine> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<SysApprovalSettingLine> findByApprovalSettingId(String approvalSettingId) {
        SysApprovalSettingLineQueryCriteria criteria=new SysApprovalSettingLineQueryCriteria();
        criteria.setApprovalSettingId(approvalSettingId);
        List<SysApprovalSettingLine> lines = queryAll(criteria);
        String userName=getUserName();
        KqDept kqDept =null;
        if(approvalSettingId.equals("3")){
            kqDept = kqDeptService.findByGroupUserName(userName);
//            if(UtilValidate.isEmpty(kqDept)){
//                throw new YmException("您不是组长，无法发起审批!");
//            }
        }

        SysApprovalSettingNodeQueryCriteria c2=new SysApprovalSettingNodeQueryCriteria();
        c2.setApprovalSettingId(approvalSettingId);
        List<SysApprovalSettingNode> nodes = sysApprovalSettingNodeService.queryAll(c2);
        for(SysApprovalSettingLine line:lines){
            List<SysApprovalSettingNode> list=new ArrayList<>();
            for(SysApprovalSettingNode node:nodes){
                if(node.getApprovalSettingLineId().equals(line.getApprovalSettingLineId())){
                    //匹配上级领导
                    // 车间考勤审批
                    if(approvalSettingId.equals("3")){
                        if(UtilValidate.isNotEmpty(node.getIzMatcheManage())&&node.getIzMatcheManage().equals(1)){
//                            if(node.getSort().equals(0)){
//                                node.setUserId(kqDept.getManageUserId());
//                                node.setUserName(kqDept.getManageUserName());
//                            }
                            if(node.getSort().equals(1)&&UtilValidate.isNotEmpty(kqDept.getGroupUserId2())){
                                node.setUserId(kqDept.getGroupUserId2());
                                node.setUserName(kqDept.getGroupUserName2());
                                list.add(node);
                            }
                            if(node.getSort().equals(2)&&UtilValidate.isNotEmpty(kqDept.getManageUserId())){
                                node.setUserId(kqDept.getManageUserId());
                                node.setUserName(kqDept.getManageUserName());
                                list.add(node);
                            }
                        }else{
                            list.add(node);
                        }
                    }else{
                        list.add(node);
                    }

                }
            }
            line.setItems(list);
        }
        return lines;
    }


    @Override
    public void saveSettingLine(SysApprovalSettingLine sysApprovalSettingLine) {

        saveOrUpdate(sysApprovalSettingLine);
        if(UtilValidate.isNotEmpty(sysApprovalSettingLine.getItems())){
            List<SysApprovalSettingNode> items = sysApprovalSettingLine.getItems();
            for(SysApprovalSettingNode item:items){
                item.setApprovalSettingId(sysApprovalSettingLine.getApprovalSettingId());
                item.setApprovalSettingLineId(sysApprovalSettingLine.getApprovalSettingLineId());
            }
            sysApprovalSettingNodeService.saveOrUpdateBatch(items);
        }
    }
}
